﻿<UserControl x:Class="GameMapView"
             x:Name="GameWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPFZ80MapEditor"
                xmlns:converters="clr-namespace:WPFZ80MapEditor.ValueConverters"
                xmlns:simconverters="clr-namespace:WPFZ80MapEditor.SimConverters"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Width="256" Height="256" Focusable="True" KeyDown="GameWindow_KeyDown" KeyUp="GameWindow_KeyUp" MouseLeftButtonDown="GameWindow_MouseLeftButtonDown" Background="Black">
    <UserControl.Resources>
        <!--<localconverters:ImageIndexConverter x:Key="convertImageIndex"/>-->
        <simconverters:ImageConverter x:Key="ImageConverter"/>
        <converters:TileConverter x:Key="TileConverter"/>
        <converters:YConverter x:Key="YConverter"/>
        <converters:XConverter x:Key="XConverter"/>
        <converters:ZConverter x:Key="ZConverter"/>
        <local:InvertEffect x:Key="InvertEffect"/>

        <simconverters:ShadowScalingConverter x:Key="ShadowScalingConverter"/>

        <DataTemplate DataType="{x:Type local:ZDrawEntry}">
            <Image x:Name="DrawEntryImage">
                <Image.Source>
                    <MultiBinding Converter="{StaticResource ImageConverter}">
                        <Binding Path="DataContext" ElementName="GameWindow"/>
                        <Binding Path="Image"/>
                    </MultiBinding>
                </Image.Source>
            </Image>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Inverted}" Value="True">
                    <Setter TargetName="DrawEntryImage" Property="Effect" Value="{StaticResource InvertEffect}"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </UserControl.Resources>

    <Canvas x:Name="CurrentMap"  Background="Transparent" IsHitTestVisible="False">
        <local:XMapView Panel.ZIndex="0" DataContext="{Binding Map}" Canvas.Left="0" Canvas.Top="0" ShowNewMaps="False" IsHitTestVisible="False"/>
        <ItemsControl Panel.ZIndex="2" ItemsSource="{Binding Path=DrawEntries}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas Width="256" Height="256"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Left">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource XConverter}">
                                <Binding Path="DataContext.Scenario" ElementName="GameWindow"/>
                                <Binding Path="Image"/>
                                <Binding Path="X"/>
                                <Binding Path="W"/>
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Canvas.Top">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource YConverter}">
                                <Binding Path="DataContext.Scenario" ElementName="GameWindow"/>
                                <Binding Path="Image"/>
                                <Binding Path="Y"/>
                                <Binding Path="H"/>
                                <Binding Path="Z"/>
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Panel.ZIndex">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource ZConverter}">
                                <Binding Path="Y"/>
                                <Binding Path="H"/>
                                <Binding Path="Z"/>
                                <Binding Path="D"/>
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ItemsControl.ItemContainerStyle>
        </ItemsControl>
        
        <ItemsControl Panel.ZIndex="1" ItemsSource="{Binding Path=DrawEntries}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas Width="256" Height="256"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Left" Value="{Binding X}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Width="{Binding W}" Height="{Binding H}" HorizontalAlignment="Center">
                        <Ellipse Width="{Binding ., Converter={StaticResource ShadowScalingConverter}, ConverterParameter=16}"
                                     Height="{Binding ., Converter={StaticResource ShadowScalingConverter}, ConverterParameter=6}"
                                     Fill="Black">
                        </Ellipse>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <ItemsControl Panel.ZIndex="1" ItemsSource="{Binding Path=AnimatedTiles}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas Width="256" Height="256"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Left" Value="{Binding X}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Width="16" Height="16">
                        <local:AnimatedTile Width="16" Height="16" 
                                            x:Name="AnimatedTileImage"
                                            MapData="{Binding DataContext.Map, ElementName=GameWindow}"
                                            >
                        </local:AnimatedTile>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <ItemsControl Panel.ZIndex="3" ItemsSource="{Binding Path=Screen}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas Width="256" Height="256"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Left" Value="{Binding X}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Panel.ZIndex="3" Width="{Binding Width}" Height="{Binding Height}" BorderThickness="1" BorderBrush="#FF4040FF" Background="Transparent" Opacity="0.7">
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <ItemsControl Panel.ZIndex="3" ItemsSource="{Binding Path=EnemyScreen}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas Width="256" Height="256"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Left" Value="{Binding X}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Panel.ZIndex="3" Width="{Binding Width}" Height="{Binding Height}" BorderThickness="1" BorderBrush="#FFDF5003" Background="Transparent" Opacity="0.7">
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Canvas>
</UserControl>
